package com.hzd.service.lol;

import ablaze.framework.utils.InetUtils;
import ablaze.framework.utils.JSONUtils;
import ablaze.framework.utils.RandomUtils;
import ablaze.framework.utils.ValidateUtils;
import com.hzd.sdk.api.PayMchAPI;
import com.hzd.sdk.bean.paymch.Sendredpack;
import com.hzd.sdk.bean.paymch.SendredpackResult;
import com.hzd.sdk.client.LocalHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author christ
 * @date 2015/11/9
 */
public class RedPackSender {

    private static Logger logger = LoggerFactory.getLogger(RedPackSender.class);

    public static String send(String billno, String title, String openid, String amount) {

        logger.info("发送红包>>billno=" + billno + "   openid=" + openid + "  amount=" + amount);
        LocalHttpClient.initMchKeyStore("1284761701", RedPackSender.class.getResource("/apiclient_cert.p12").getFile());
        PayMchAPI api = new PayMchAPI();

        Sendredpack sendredpack = new Sendredpack()
                .setMch_billno(billno)
                .setNonce_str(RandomUtils.randomNumeric(20))
                .setMch_id("1284761701")
                .setWxappid("wxb3b979f9368403c8")
                .setSend_name("互助君")
                .setRe_openid(openid)
                .setTotal_amount(Integer.valueOf(amount))
                .setTotal_num(1)
                .setWishing("恭喜你撸败了全国51%的对手，获得了“" + title + "”之称号")
                .setClient_ip(InetUtils.getHostAddress())
                .setAct_name("光棍万岁,狂撸抢钱活动")
                .setRemark("恭喜你");

        SendredpackResult result = api.mmpaymkttransfersSendredpack(sendredpack, "72c472a5ada0ff98c8069159c5e82325");

        if ("SUCCESS".equals(result.getResult_code()) && ValidateUtils.isEmpty(result.getErr_code())) {
            return result.getSend_listid();
        }

        //发送红包失败
        logger.error("发送红包失败>>billno=" + billno + "   openid=" + openid + "  amount=" + amount);
        logger.error(JSONUtils.serialize(result));
        return null;
    }
}
